<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>安装 wordpress（LAMP）</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>安装 wordpress（LAMP）</h1>    <p>
        under
            <a href="../../tags/apache/">apache</a>
            <a href="../../tags/mysql/">mysql</a>
            <a href="../../tags/wordpress/">wordpress</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2019-06-09</p>


    <h1>安装 wordpress（LAMP）</h1>
<p>在 Debian 9 amd64 上测试成功。</p>
<h2>安装和配置 LAMP</h2>
<h3>MySQL</h3>
<p>输入以下命令安装 mysql-server：</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install mysql-server
</code></pre></div>


<p>安装完成后，运行以下命令进行安全设置：</p>
<div class="highlight"><pre><span></span><code>sudo mysql_secure_installation
</code></pre></div>


<p>以 root 身份启动 mysql 命令行：</p>
<div class="highlight"><pre><span></span><code>sudo mysql -u root -p
</code></pre></div>


<p>创建和设置 wordpress 数据库和及其用户（替换 &lt;&gt; 中的内容）：</p>
<div class="highlight"><pre><span></span><code><span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="o">&lt;</span><span class="n">DB_NAME</span><span class="o">&gt;</span> <span class="k">DEFAULT</span> <span class="nb">CHARACTER</span> <span class="k">SET</span> <span class="n">utf8mb4</span> <span class="k">COLLATE</span> <span class="n">utf8mb4_unicode_ci</span><span class="p">;</span>

<span class="k">CREATE</span> <span class="k">USER</span> <span class="ss">&quot;&lt;DB_USER&gt;&quot;</span><span class="o">@</span><span class="ss">&quot;localhost&quot;</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="ss">&quot;&lt;PASSWORD&gt;&quot;</span><span class="p">;</span>

<span class="k">GRANT</span> <span class="k">ALL</span> <span class="k">PRIVILEGES</span> <span class="k">ON</span> <span class="o">&lt;</span><span class="n">DB_NAME</span><span class="o">&gt;</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="ss">&quot;&lt;DB_USER&gt;&quot;</span><span class="o">@</span><span class="ss">&quot;localhost&quot;</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="ss">&quot;&lt;DB_PASSWORD&gt;&quot;</span><span class="p">;</span>

<span class="n">FLUSH</span> <span class="k">PRIVILEGES</span><span class="p">;</span>
</code></pre></div>


<p>根据<a href="https://stackoverflow.com/a/766996/9634290">这篇讨论</a>，应该使用 utf8mb4 字符集和 utf8mb4_unicode_ci。</p>
<h3>php</h3>
<p>安装 php 以及基本模块：</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
</code></pre></div>


<p>安装常用的 php 扩展：</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install php-curl php-gd php-mbstring php-xml php-xmlrpc
</code></pre></div>


<h3>apache</h3>
<p>输入以下命令安装 apache2：</p>
<div class="highlight"><pre><span></span><code>sudo apt-get install apache2
</code></pre></div>


<h2>设置 wordpress</h2>
<p>下载最新的 <a href="https://wordpress.org/download/">wordpress</a>（目前是5.1.1）。将 wordpress 解压到 /path/to/wordpress，确保顶级 index.php 脚本位于该目录下。</p>
<div class="highlight"><pre><span></span><code>wget -O - https://wordpress.org/latest.tar.gz <span class="p">|</span> tar zxf -
</code></pre></div>


<h3>配置 wordpress</h3>
<p>进入 /path/to/wordpress 目录，将 wp-config-sample.php 重命名为 wp-config.php，将 <code>DB_NAME</code>、<code>DB_USER</code>、<code>DB_PASSWORD</code>、<code>DB_HOST</code>、<code>DB_CHARSET</code> 和 <code>DB_COLLATE</code> 修改为 MySQL 数据库中设置的值。</p>
<p>然后访问 <a href="https://api.wordpress.org/secret-key/1.1/salt">https://api.wordpress.org/secret-key/1.1/salt</a>，用这个网页生成的随机代码替换配置文件中的相应内容：</p>
<div class="highlight"><pre><span></span><code><span class="x">define( &#39;AUTH_KEY&#39;,         &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;SECURE_AUTH_KEY&#39;,  &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;LOGGED_IN_KEY&#39;,    &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;NONCE_KEY&#39;,        &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;AUTH_SALT&#39;,        &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;SECURE_AUTH_SALT&#39;, &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;LOGGED_IN_SALT&#39;,   &#39;put your unique phrase here&#39; );</span>
<span class="x">define( &#39;NONCE_SALT&#39;,       &#39;put your unique phrase here&#39; );</span>
</code></pre></div>


<h3>允许 wordpress 修改站点目录的内容</h3>
<p>将 wordpress 目录下的所有目录和文件的用户组设置为 www-data：</p>
<div class="highlight"><pre><span></span><code>sudo chown -R &lt;USER&gt;:www-data /path/to/wordpress
</code></pre></div>


<p>确保 wordpress 目录下新创建的文件和目录的用户组是 www-data：</p>
<div class="highlight"><pre><span></span><code>sudo find /path/to/wordpress/wp-<span class="o">{</span>admin,content,includes<span class="o">}</span> -type d -exec chmod g+s <span class="o">{}</span> <span class="se">\;</span>
</code></pre></div>


<p>允许 www-data 组的用户在 wp-content 下创建和删除文件或目录：</p>
<div class="highlight"><pre><span></span><code>sudo find /path/to/wordpress/wp-content -type d -exec chmod g+w <span class="o">{}</span> <span class="se">\;</span>
</code></pre></div>


<p>设置 wordpress 直接读写文件系统，在 wp-config.php 末尾加上：</p>
<div class="highlight"><pre><span></span><code><span class="x">define(&#39;FS_METHOD&#39;, &#39;direct&#39;);</span>
</code></pre></div>


<p>但是根据 <a href="https://codex.wordpress.org/Editing_wp-config.php">wordpress 文档</a>，此为默认设置。</p>
<h3>启用 .htaccess 配置</h3>
<p>wordpress 用 .htaccess 来<a href="https://codex.wordpress.org/htaccess">实现子目录级别的配置</a>，例如实现更加友好的 Permalink。在网站的管理员不能直接修改 apache 配置的情况下，可以<a href="https://stackoverflow.com/a/13170837">用这种方式来设置其网站</a>。但这种方式也带来性能开支，所以 <a href="https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/">nginx 没有采用</a>。<a href="https://codex.wordpress.org/Nginx">wordpress 文档</a>列出了使用 nginx 作为 web 服务器带来的限制。</p>
<p>在 wordpress 目录下添加 .htaccess 文件：</p>
<div class="highlight"><pre><span></span><code>touch /path/to/wordpress/.htaccess
sudo chown &lt;USER&gt;:www-data /path/to/wordpress/.htaccess
chmod <span class="m">660</span> <span class="nv">$HT_PATH</span>
</code></pre></div>


<p>修改 wordpress 目录的安全设置，在 /etc/apache2/apache2.conf
文件中添加以下语句：</p>
<div class="highlight"><pre><span></span><code><span class="nt">&lt;Directory</span> <span class="s">/path/to/wordpress/</span><span class="nt">&gt;</span>
    <span class="nb">Options</span> Indexes FollowSymLinks
    <span class="nb">AllowOverride</span> <span class="k">All</span>
    <span class="nb">Require</span> <span class="k">all</span> granted
<span class="nt">&lt;/Directory&gt;</span>
</code></pre></div>


<p>确保 apache2 的 rewrite 模块已经启用：</p>
<div class="highlight"><pre><span></span><code>sudo a2enmod rewrite
</code></pre></div>


<p>重启 apache2 服务来应用修改：</p>
<div class="highlight"><pre><span></span><code>sudo systemctl restart apache2
</code></pre></div>


<h3>启用 wordpress 站点</h3>
<p>修改 apache2 站点配置指向 /path/to/wordpress 目录：</p>
<div class="highlight"><pre><span></span><code><span class="nt">&lt;VirtualHost</span> <span class="s">*:80</span><span class="nt">&gt;</span>
    <span class="nb">DocumentRoot</span> <span class="sx">/path/to/wordpress</span>
<span class="nt">&lt;/VirtualHost&gt;</span>
</code></pre></div>


<p>重启 apache2 服务来应用修改：</p>
<div class="highlight"><pre><span></span><code>sudo systemctl restart apache2
</code></pre></div>


<h3>升级 wordpress</h3>
<p>创建 wordpress 升级目录 /path/to/wordpress/wp-content/upgrade：</p>
<div class="highlight"><pre><span></span><code>mkdir /path/to/wordpress/wp-content/upgrade
chmod g+w /path/to/wordpress/wp-content/upgrade
sudo chown &lt;USER&gt;:www-data /path/to/wordpress/wp-content/upgrade
</code></pre></div>


<p>当有可用更新时，将 wordpress 目录及其所有内容的用户设置为 www-data：</p>
<div class="highlight"><pre><span></span><code>sudo chown -R www-data /path/to/wordpress
</code></pre></div>


<p>更新完成后再将改回原来的用户名：</p>
<div class="highlight"><pre><span></span><code>sudo chown -R &lt;USER&gt; /path/to/wordpress
</code></pre></div>


<h2>参考资料</h2>
<ul>
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04">LAMP 安装教程</a></li>
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lamp-on-ubuntu-16-04">在 Ubuntu 16.04 上基于 LAMP 安装 wordpress</a></li>
</ul>
    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>